package com.liu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liu.entity.Doctor;
import com.liu.service.DoctorService;
import com.liu.vo.DataView;
import com.liu.vo.DoctorVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
public class DoctorController {

    @Autowired
    private DoctorService doctorService;

    @RequestMapping("/toDoctor")
    public String toDoctor(){
        return "admin/doctor";
    }

    /**
     *    模糊查询所有医生 带有分页
     */

    @RequestMapping("/listDoctor")
    @ResponseBody
    public DataView listDoctor(DoctorVo doctorVo){
        IPage<Doctor> page = new Page<>(doctorVo.getPage(),doctorVo.getLimit());
        QueryWrapper<Doctor> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(doctorVo.getName() != null, "name", doctorVo.getName());
        queryWrapper.like(doctorVo.getDepartment() != null, "department", doctorVo.getDepartment());
        doctorService.page(page,queryWrapper);

        DataView dataView = new DataView(page.getTotal(),page.getRecords());
        return dataView;
    }

    /**
     * 增加医生
     * @param doctor
     * @return
     */
    @RequestMapping("/addDoctor")
    @ResponseBody
    public DataView addDoctor(Doctor doctor){
        DataView dataView = new DataView();
        boolean b = doctorService.saveOrUpdate(doctor);
        if(b){
            dataView.setCode(200);
            dataView.setMsg("医生增加或者更改成功");
            return dataView;
        }
        dataView.setCode(100);
        dataView.setMsg("医生增加或者更改失败");
        return dataView;
    }

    /**
     * 删除医生
     */

    @RequestMapping("/deleteDoctor")
    @ResponseBody
    public DataView deleteDoctor(Integer id){
        DataView dataView = new DataView();
        boolean b = doctorService.removeById(id);

        if(b){
            dataView.setCode(200);
            dataView.setMsg("医生删除成功");
            return dataView;
        }
        dataView.setCode(100);
        dataView.setMsg("医生删除失败");

        return dataView;
    }
}
